<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
    <el-button class="miui-dialog-close-btn" @click="visible = false">关闭页面</el-button>
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="学校类型" prop="schoolType">

        <el-select v-model="dataForm.schoolType" placeholder="请选择学校类型" @change="schoolTypeChange">
          <el-option v-for="item in schoolTypeOptions" :label="item.label" :value="item.value" />
        </el-select>

      </el-form-item>
      <el-form-item label="年级" prop="gradeName">
        <el-select v-model="dataForm.gradeId" @change="bindGradeLabel" placeholder="年级">
          <el-option v-for="item in gradeList" :key="item.id" :label="item.gradeName" :value="item.id" />
        </el-select>
      </el-form-item>

      <el-form-item label="性别" prop="gender">
        <el-select v-model="dataForm.gender" placeholder="请选择">
          <el-option label="男" value="0"></el-option>
          <el-option label="女" value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="低体重" prop="lowWeight">
        <el-row>
          <el-col :span="20">
            <el-input v-model="dataForm.lowWeight" placeholder="低体重"></el-input>
          </el-col>
        </el-row>
      </el-form-item>

      <el-form-item label="正常值" prop="normalStart">
        <el-row>
          <el-col :span="10">
            <el-input v-model="dataForm.normalStart" placeholder="正常值开始"></el-input>
          </el-col>
          <el-col class="line" :span="1">-</el-col>
          <el-col :span="10">
            <el-input v-model="dataForm.normalEnd" placeholder="正常值结束"></el-input>
          </el-col>
        </el-row>
      </el-form-item>

      <el-form-item label="超重" prop="overweightStart">
        <el-row>
          <el-col :span="10">
            <el-input v-model="dataForm.overweightStart" placeholder="超重开始"></el-input>
          </el-col>
          <el-col class="line" :span="1">-</el-col>
          <el-col :span="10">
            <el-input v-model="dataForm.overweightEnd" placeholder="超重结束"></el-input>
          </el-col>
        </el-row>
      </el-form-item>

      <el-form-item label="肥胖" prop="obesity">
        <el-row>
          <el-col :span="20">
            <el-input v-model="dataForm.obesity" placeholder="肥胖"></el-input>
          </el-col>
        </el-row>
      </el-form-item>

    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" v-prevent-re-click>确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import { mapGetters } from 'vuex'
  import { remote } from '@/api/admin/dict'
  import { getObj, addObj, putObj } from '@/api/admin/nutrition'
  import { getGradeBySchoolType } from '@/api/pvm/grade'
  
  export default {
    data() {
      return {
        visible: false,
        gradeList: [],
        schoolTypeOptions: [],
        dataForm: {
          id: 0,
          schoolType: '',
          gradeId: '',
          gradeName: '',
          gender: '',
          lowWeight: '',
          normalStart: '',
          normalEnd: '',
          overweightStart: '',
          overweightEnd: '',
          obesity: ''
        },
        dataRule: {
          schoolType: [{required: true,message: '学校类型不能为空',trigger: 'blur'}],
          gradeId: [{required: true,message: '年级不能为空',trigger: 'blur'}],
          gender: [{required: true,message: '性别不能为空',trigger: 'blur'}],
          lowWeight: [{required: true,message: '低体重不能为空',trigger: 'blur'}],
          normalStart: [{required: true,message: '正常值开始不能为空',trigger: 'blur'}],
          normalEnd: [{required: true,message: '正常值结束不能为空',trigger: 'blur'}],
          overweightStart: [{required: true,message: '超重开始不能为空',trigger: 'blur'}],
          overweightEnd: [{required: true,message: '超重结束不能为空',trigger: 'blur'}],
          obesity: [{required: true,message: '肥胖不能为空',trigger: 'blur'}],
        }
      }
    },
    created() {
      this.getGradeList()
    },
    computed: {
      ...mapGetters(['bizData', 'keyCollapse'])
    },
    methods: {
      init(id) {
        this.dataForm.id = id || 0
        remote('school_type').then(result => {
          this.schoolTypeOptions = result.data.data
        })
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            getObj(this.dataForm.id).then(response => {
              this.dataForm = response.data.data
            })
          }
        })
      },
      bindGradeLabel(value) {
        let obj = {};
        obj = this.gradeList.find((item) => {
          return item.id === value;
        });
        this.dataForm.gradeName = obj.gradeName;

      },
      schoolTypeChange(value) {
        this.dataForm.gradeId = "";
        this.gradeList = [];
        getGradeBySchoolType(value).then(response => {
          this.gradeList = response.data.data;
        })
      },
      // 表单提交
      dataFormSubmit() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.dataForm.id) {
              putObj(this.dataForm).then(data => {
                this.$message.success('修改成功')
                this.visible = false
                this.$emit('refreshDataList')
              });
            } else {
              addObj(this.dataForm).then(data => {
                this.$message.success('添加成功')
                this.visible = false
                this.$emit('refreshDataList')
              })
            }
          }
        })
      },
      getGradeList() {
        getGradeBySchoolType().then(response => {
          this.gradeList = response.data.data
        })
      },
    }
  }
</script>

<style lang="scss">
  .line {
    text-align: center;
  }
</style>
